package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsHomeRecommendSubject;
import cn.zcscloud.mall.service.SmsHomeRecommendSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页专题推荐管理Controller
 * Created by macro on 2018/11/6.
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/home/recommendSubject")
public class SmsHomeRecommendSubjectController {
    @Autowired
    private SmsHomeRecommendSubjectService recommendSubjectService;

    /**
     * 添加首页推荐专题
     * @param homeRecommendSubjectList
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody List<SmsHomeRecommendSubject> homeRecommendSubjectList){
        boolean b = recommendSubjectService.saveBatch(homeRecommendSubjectList);
        if (b){
            return R.success("操作成功");
        }
        return R.error();
    }

    /**
     * 批量删除推荐
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        recommendSubjectService.removeByIds(ids);
        return R.success("操作成功");
    }

    /**
     * 分页展示
     * @param subjectName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(value = "subjectName", required = false) String subjectName,
                                                                  @RequestParam(value = "recommendStatus", required = false) Integer recommendStatus,
                                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(subjectName != null,SmsHomeRecommendSubject::getSubjectName, subjectName)
                .eq(recommendStatus != null,SmsHomeRecommendSubject::getRecommendStatus, recommendStatus);
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum, pageSize);
        Page<SmsHomeRecommendSubject> iPage = recommendSubjectService.page(page, queryWrapper);
        return R.success(PageRequest.init(iPage));
    }

    @PostMapping("/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam("ids") List<Long> ids, @RequestParam Integer recommendStatus){
        int count = 0;
        SmsHomeRecommendSubject smsHomeRecommendSubject = new SmsHomeRecommendSubject();
        smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
        for (Long id : ids) {
            smsHomeRecommendSubject.setId(id);
            recommendSubjectService.updateById(smsHomeRecommendSubject);
            count++;
        }
        if (count > 0) {
            return R.success("操作成功");
        }
        return R.error();
    }

    @PostMapping("/update/sort/{id}")
    public R updateRecommendSort(@PathVariable Long id,Integer sort){
        UpdateWrapper<SmsHomeRecommendSubject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SmsHomeRecommendSubject::getRecommendStatus, id);
        SmsHomeRecommendSubject recommendSubject = new SmsHomeRecommendSubject();
        recommendSubject.setSort(sort);
        recommendSubject.setId(id);
        boolean update = recommendSubjectService.updateById(recommendSubject);
        if(!update){
            return R.error();
        }
        return R.success("操作成功");
    }

}
